import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/logout")
public class Logout extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置响应和请求格式
        req.setCharacterEncoding("utf8");
        resp.setContentType("text/html; charset=utf8");
        //1.获取会话
        HttpSession session = req.getSession();
        if(session == null) {
            resp.setStatus(403);
            resp.getWriter().write("当前未登录！");
            return;
        }
        //2.获取user对象
        User user = (User) session.getAttribute("user");
        if(user == null) {
            resp.setStatus(403);
            resp.getWriter().write("当前未登录！");
            return;
        }
        //3.删除user对象
        session.removeAttribute("user");
        //4.重定向到登录页
        resp.sendRedirect("login.html");
    }
}
